<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<style type="text/css">
			#provider {
				margin-left: 16%;
			}
		</style>
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr style="display: none;">
						<td width="20%">
							ID
						</td>
						<td width="50%">
							<input class="easyui-textbox" id="input_ID" name="ID" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>出库单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>销售单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_SALESORDER_ID" name="SALESORDER_ID" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">*</h>用户组
						</td>
						<td width="30%">
							<input class="easyui-combobox" id="input_CUSTOMER_TYPE" name="CUSTOMER_TYPE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>配送单位
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_CUSTOMER_NAME" name="CUSTOMER_NAME" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">* </h>配送时间
						</td>
						<td width="30%">
							<input class="easyui-datebox" id="input_DISTRIBUTION_DATE" name="DISTRIBUTION_DATE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
				</table>

				<table id="tt">

				</table>
				<div style="padding: 5px 0px ;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" class="print_text_style" style="border: none;" name="number" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" class="print_text_style" style="border: none; width: 500px;" id="dx" />
								<div id="dx"></div>
							</td>
						</tr>
					</table>
					<div style="margin-left: 20%;" id="new_footer">

					</div>
				</div>
			</form>
		</div>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/json2.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
	</body>
	<script type="text/javascript">
		var auth = "admin;" + hex_sha1("admin1");
		var id = getID(); //保存获取的ID
		var updataData; //保存深度拷贝后的数据
		$(document).ready(function() {
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			} else if(id == undefined) {
				currid = AddFlag;
			}
		});

		addFooter($("#new_footer"));
		var receipts_type = RECEIPT_SAVE_SYMBOL + RECEIPT_SEARCH_SYMBOL;
		var content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		var _detailToolbarData = AddDetailToolBar(2);
		GetTable();

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: 'JSON',
				success: function(result) {
					if(result != null) {
						updataData = deepCopy(result);
						updataData.DISTRIBUTION_DATE = updataData.DISTRIBUTION_DATE.replace(/-/g, "/");
						getFormData(updataData);
						var _data = updataData.rows;
						_data = changeRealData(_data);
						_data = toFixTwo(_data);
						$('#tt').datagrid('loadData', _data);
						CalSum();
					}
				},
				error: function(result) {
					$.messager.alert("消息", "请求失败");
				}
			});
		}

		function loadRemote(_url) {
			var _url = RemoteLinkAddress + '/logistics/salesorder/findbyid.do?ID=' + id;
			getRemoteData(_url);
		}

		function save() {
			var a = checkDataIntegrity();
			if(a == 0) {
				$.messager.alert("提示", "请完成明细中的数据再进行保存！");
				return;
			}
			getSaveData();
		}

		function searchmore() {
			AddTabIn("销售结算", parent.$("#tabs"), "UI/LOGISTICS/SalesSettlementOrder/SalesSettlementSearch.html");
		}

		function GetTable() {
			$('#tt').edatagrid({
				height: 310,
				toolbar: _detailToolbarData,
				collapsible: true,
				singleSelect: true,
				fitColumns: true,
				autoSave: true,
				columns: [
					[{
						field: 'INTERNAL_NAME',
						title: '内部商品名',
						width: '10%',
						align: 'center',

					}, {
						field: 'UNIT_NAME',
						title: '原始单位',
						width: '5%',
						align: 'center',

					},{
						field: 'UNIT_PRICE',
						title: '计划单价',
						width: '10%',
						align: 'center',
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}, {
						field: 'REAL_PRICE',
						title: '实际单价',
						width: '10%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
								precision: 2, //保留两位
								groupSeparator: ',', //每组用','隔开
								decimalSeparator: '.',
								prefix: '￥' //在数据前面加￥符号
							}

						},
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}, {
						field: 'AMOUNT',
						title: '数量',
						width: '11%',
						align: 'center',
						formatter: function(value, row, index) {
							if(value) {
								return value;
							}
						}
					}, {
						field:'UNIT_NAME',
						title: '原始单位',
						width: '5%',
						align: 'center',
					}, {
						field: 'REAL_AMOUNT',
						title: '实收数量',
						width: '11%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
								precision: 2,
								groupSeparator: ',',
								decimalSeparator: '.',
							}
						},

					}, {
						field:'INOUT_UNIT',
						title: '单位',
						width: '5%',
						align: 'center',
					},{
						field: 'REAL_SUM',
						title: '总价',
						width: '11%',
						align: 'center',
						formatter: function(value, row, index) {
							if(value) {
								var m = parseFloat(value);
								m = m.toFixed(2);
								return "￥" + m;
							}
						}
					}, {
						field: 'PROCESSING_MODE',
						title: '退货处理',
						width: '8%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: 'text',
								textField: 'text',
								data: [{
									'text': '收货'
								}, {
									'text': '退货未退回'
								}, {
									'text': '退货入库'
								}]
							}
						}
					}, {
						field: 'RETURN_AMOUNT',
						title: '退货数量',
						width: '8%',
						align: 'center',
						formatter: function(value, row, index) {
							return value;
						},
						editor: {
							type: 'numberbox',
						}
					}]
				],
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					var amount = row.REAL_AMOUNT; //将该行name为AMOUNT的数据赋值给amount
					var perprice = row.REAL_PRICE; //同上
					var result = parseFloat(amount) * parseFloat(perprice); //将上述两个的值相乘再复制给result
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							REAL_SUM: result.toFixed(2)
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					CalSum();

				}
			});
		}

		function getSaveData() {
			var _dataPage = submitFormData();
			_dataPage.SALESORDER_ID = updataData.ID;
			_dataPage.rows = updataData.rows;
			_dataPage.FINALSUM = $("input[name='number']").val();
			var _data = JSON.stringify(_dataPage);
			submitData(_data);
		}

		function submitData(_data) {
			var _url = "";
			_url = RemoteLinkAddress + "/logistics/receiveorder/save.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _data,
				type: "POST",
				success: function(result) {
					if(result.msg.search("success") > -1) {
						$.messager.alert({
							title: '成功',
							msg: '保存成功',
							fn: function() {
								parent.$('#tabs').tabs('close', "新增销售结算");

							}
						});
					} else {
						$.messager.alert("提示信息", "新增失败");
					}
				},
				error: function(result) {
					$.messager.alert('提示信息', '新增失败');
				}
			});
		}

		function CalSum() {
			var _num = 0;
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				_num += parseFloat(dataGridRows[i].REAL_SUM);
			}
			_num = _num.toFixed(2);
			var num = changeMoneyToChinese(_num);
			$("#dx").val(num);
			$("input[name='number']").val(_num);
		}

		//将预计值赋给实际值
		function changeRealData(data) {
			for(var s in data) {
				if(data[s].REAL_PRICE == 0 || data[s].REAL_PRICE == undefined || data[s].REAL_PRICE === "") {
					data[s].REAL_PRICE = data[s].UNIT_PRICE;
				}
				if(data[s].REAL_AMOUNT == 0 || data[s].REAL_AMOUNT == undefined || data[s].REAL_AMOUNT === "") {
					data[s].REAL_AMOUNT = data[s].AMOUNT;
				}
				data[s].REAL_SUM = data[s].REAL_PRICE * data[s].REAL_AMOUNT;
			}
			return data;
		}

		function checkDataIntegrity() {
			var rowsdata = $("#tt").datagrid('getRows');
			if(rowsdata.length != 0) {
				for(var i = 0; i < rowsdata.length; i++) {
					var internalname = rowsdata[i].INTERNAL_NAME;
					var unitname = rowsdata[i].UNIT_NAME;
					var unitprice = rowsdata[i].UNIT_PRICE;
					var realprice = rowsdata[i].REAL_PRICE;
					var amount = rowsdata[i].AMOUNT;
					var realamount = rowsdata[i].REAL_AMOUNT;
					var realsum = rowsdata[i].REAL_SUM;
					var processingmode = rowsdata[i].PROCESSING_MODE;
					if(internalname == "" || unitname == "" || unitprice == "" || realprice == "" || amount == "" || realamount == "" || realsum == "" || processingmode == "") {
						return 0;
					}
				}
			} else {
				return 0;
			}
		}

		function toFixTwo(data) {
			for(var singleData of data) {
				singleData.WAREHOUSEOUT_SUM = singleData.WAREHOUSEOUT_SUM.toFixed(1);
			}
			return data;
		}
	</script>

</html>